package com.example.widget_lib.callback;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.IDataAbilityObserver;
import ohos.eventhandler.EventHandler;
import ohos.sysappcomponents.settings.SystemSettings;
import ohos.utils.net.Uri;

import java.lang.ref.WeakReference;

public class SettingsSystemObserver implements IDataAbilityObserver {


    public String mName;
    private WeakReference<Callback> mCallback;

    @Override
    public void onChange() {
        Callback callback = getCallback();
        if (callback != null) {
            callback.mtcSettingsSystemChanged(this);
        }
    }

    public interface Callback {
        void mtcSettingsSystemChanged(SettingsSystemObserver observer);
    }

    public SettingsSystemObserver(EventHandler handler) {
        super();
        //   super(handler);
    }

    public void setCallback(Callback callback) {
        mCallback = (callback == null) ? null : new WeakReference<Callback>(callback);
    }

    public Callback getCallback() {
        return (mCallback == null) ? null : mCallback.get();
    }

    public void register(DataAbilityHelper contentResolver, String name) {
        mName = name;
        Uri uri = SystemSettings.getUri(name);
        contentResolver.registerObserver(uri,  this);
    }

    public void unregister(DataAbilityHelper contentResolver, String name) {
        Uri uri = SystemSettings.getUri(name);
        contentResolver.unregisterObserver(uri, this);
    }

    public void onChange(boolean selfChange) {
        Callback callback = getCallback();
        if (callback != null) {
            callback.mtcSettingsSystemChanged(this);
        }
    }
}
